package com.xinnet.mms.goods.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.mms.allot.dao.CustomAllotDao;
import com.xinnet.mms.entity.Goods;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.enums.ProductTypeEnum;
import com.xinnet.mms.goods.dao.GoodsDao;
import com.xinnet.mms.goods.service.GoodsService;
import com.xinnet.mms.utils.CustomerManage;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService{

	@Autowired
	GoodsDao goodsDao;
	@Autowired
	CustomAllotDao customAllotDao;
	/**
	 * 从商品中心取得所有product_class
	 */
	@Override
	public List<Product> getProductClassFromGoodsCenter() throws SQLException {
		return goodsDao.getProductClassFromGoodsCenter();
	}
	
	/**
	 * 根据商品codes获取商品
	 */
	@Override
	public Goods getGoodsByGoodsCodes(String goodsCodes) throws SQLException {
		return goodsDao.getGoodsByGoodsCodes(goodsCodes);
	}

	/**
	 * 根据会员ID获取该会员所有的购物车商品顶级分类
	 */
	@Override
	public String[] getProductClassByCusId(String cusId) throws SQLException {
		String[] ids=new String[500];
		String productCode;
		String parentProductCode;
		
		List<Product> list=goodsDao.getProductClassByCusId(cusId);
		if(list==null || list.isEmpty()) {
			return null;
		}
		else {
			for(int i=0;i<list.size();i++) {
				productCode=list.get(i).getProductCode();
				parentProductCode=list.get(i).getParentProductId();
				//如果拿到的产品code是CHFW(云主机) 则强制设置为NYZJ(本系统内能识别的云主机CODE)
				if(productCode.equals(CustomerManage.CHFW)) {
					ids[i]=ProductTypeEnum.Cloud.getValue();
				}
				//如果拿到的产品code是ZJTG(主机托管) 则强制设置为ZJZY(主机租用)
				else if(productCode.equals(CustomerManage.ZJTG)) {
					ids[i]=ProductTypeEnum.Hosting.getValue();
				}
				else if(productCode.equals(CustomerManage.MEJZ)) {
					ids[i]=ProductTypeEnum.Jianzhan.getValue();
				}
				else if(customAllotDao.getProductByProductCode(productCode)!=null) {
					ids[i]=productCode;
				}
				//否则设置产品CODE为当前取得的产品CODE的父级CODE
				else {
					ids[i]=parentProductCode;
				}
			}
		}
		return ids;
	}
	
}
